# -*- coding: utf-8 -*-
import scrapy


class SguideSpider(scrapy.Spider):
    name = 'sguide'
    allowed_domains = ['https://news.sina.com.cn']
    start_urls = ['http://news.sina.com.cn/guide/']

    def parse(self, response):
        dictlist = response.selector.css("div.section")
        # 获取第一层的目录
        num1 = 0
        for dict in dictlist:
            num1 +=1
            dict1 = dict.css("h2.tit01::text").extract()
            print("%d." % (num1), ''.join(dict1))
            print("=" * 70)
            num2 = 0
            # 获取第二层目录
            for dict21 in dict.css("div.clearfix"):
                num2 +=1
                dict2 = dict21.css("h3.tit02 a::text").extract_first()
                if (dict2 is None):
                    dict2 = dict21.css("h3.tit02 span::text").extract_first()
                    if (dict2 is None):
                        dict2 = dict21.css("h3.tit02::text").extract_first()
                print("%d.%d" % (num1,num2), dict2)
                num3 = 0
                # 获取第三层目录
                for dict31 in dict.css("ul.list01"):
                    num3 += 1
                    dict3 = dict31.css("li a::text").extract_first()
                    print("%d.%d.%d" % (num1, num2, num3), dict3)
            print("=" * 70)
